//
//  NEDFormService.m
//  NEDFormView_Example
//
//  Created by 000 on 2022/1/7.
//  Copyright © 2022 000. All rights reserved.
//

#import "NEDFormService.h"
#import "NEDFormSeationView.h"
#import "NEDFormIconAndTitleView.h"

NEDFormItem * NEDFormInit(void){
    NEDFormItem * item = [NEDFormItem new];
    return item;
}

/// 空行
NEDFormItem * NEDFormInitSpace(CGFloat height){
    NEDFormItem * item = [NEDFormItem new];
    item.layout.height = height;
    item.layout.backgroundColor = [UIColor clearColor];
    item.layout.showBottomSeparatorLine = NO;
    return item;
}

/// 中间位置的 标题
NEDFormItem * NEDFormInitTitleCenter(NSString * title){
    NEDFormItem * item = NEDFormInit()
        .initCenter(NEDFormContentShowType_Title)
        .center.title(title);
    [item showCenterBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        UILabel * label = (UILabel *)contentView;
        label.text = contentItem.contentTitle;
    }];
    return item;
}
/// 左边位置的 标题
NEDFormItem * NEDFormInitTitleLeft(NSString * title){
    NEDFormItem * item = NEDFormInit()
        .initLeft(NEDFormContentShowType_Title)
        .left.title(title);
    [item showLeftBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        UILabel * label = (UILabel *)contentView;
        label.text = contentItem.contentTitle;
    }];
    return item;
}
/// 两边位置的 标题 副标题
UIKIT_EXTERN NEDFormItem * NEDFormInitTitleLeftRight(NSString * title, NSString * subTitle){
    NEDFormItem * item = NEDFormInit()
        .initLeft(NEDFormContentShowType_Title)
        .initRight(NEDFormContentShowType_Title)
        .left.title(title)
        .right.title(subTitle);
    [item showLeftBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        UILabel * label = (UILabel *)contentView;
        label.text = contentItem.contentTitle;
    }];
    [item showRightBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        UILabel * label = (UILabel *)contentView;
        label.text = contentItem.contentTitle;
    }];
    return item;
}

/// 左侧 图标+标题 显示 图片是图片名字或者链接
UIKIT_EXTERN NEDFormItem * NEDFormInotLeftIconAndTitle(NSString * title, NSString * imageURL){
    NEDFormItem * item = NEDFormInit()
        .initLeft(NEDFormContentShowType_Custom);
    [item initViewLeftBlock:^UIView * _Nonnull(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        NEDFormIconAndTitleView * view = [NEDFormIconAndTitleView new];
        
        return view;
    }];
    [item showLeftBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        
    }];
    return item;
}


@implementation NSArray(NEDForm)

/// 左边距
- (NSArray<NEDFormItem *> *(^)(CGFloat margin))form_marginLeft{
    NSArray *(^block)(CGFloat margin) = ^NSArray *(CGFloat margin) {
        for(NEDFormItem * item in self){
            if([item isKindOfClass:NEDFormItem.class]){
                if(!item.layout.isNotUpdateLayoutFromArrayUnifiedSettings){
                    item.marginLeft(margin);
                }
            }
        }
        return self;
    };
    return block;
}

/// 右边距
- (NSArray<NEDFormItem *> *(^)(CGFloat margin))form_marginRight{
    NSArray *(^block)(CGFloat margin) = ^NSArray *(CGFloat margin) {
        for(NEDFormItem * item in self){
            if([item isKindOfClass:NEDFormItem.class]){
                if(!item.layout.isNotUpdateLayoutFromArrayUnifiedSettings){
                    item.marginRight(margin);
                }
            }
        }
        return self;
    };
    return block;
}

/// 高度
- (NSArray<NEDFormItem *> *(^)(CGFloat height))form_height{
    NSArray *(^block)(CGFloat height) = ^NSArray *(CGFloat height) {
        for(NEDFormItem * item in self){
            if([item isKindOfClass:NEDFormItem.class]){
                if(!item.layout.isNotUpdateLayoutFromArrayUnifiedSettings){
                    item.height(height);
                }
            }
        }
        return self;
    };
    return block;
}

/// 左边视图宽度 中间的自己适配
- (NSArray<NEDFormItem *> *(^)(CGFloat width))form_widthLeft{
    NSArray *(^block)(CGFloat width) = ^NSArray *(CGFloat width) {
        for(NEDFormItem * item in self){
            if([item isKindOfClass:NEDFormItem.class]){
                if(!item.layout.isNotUpdateLayoutFromArrayUnifiedSettings){
                    item.widthLeft(width);
                }
            }
        }
        return self;
    };
    return block;
}
/// 右边视图宽度 中间的自己适配
- (NSArray<NEDFormItem *> *(^)(CGFloat width))form_widthRight{
    NSArray *(^block)(CGFloat width) = ^NSArray *(CGFloat width) {
        for(NEDFormItem * item in self){
            if([item isKindOfClass:NEDFormItem.class]){
                if(!item.layout.isNotUpdateLayoutFromArrayUnifiedSettings){
                    item.widthRight(width);
                }
            }
        }
        return self;
    };
    return block;
}

/// 隐藏底部分割线条
- (NSArray *)form_hiddenBottomLine{
    for(NEDFormItem * item in self){
        if([item isKindOfClass:NEDFormItem.class]){
            if(!item.layout.isNotUpdateLayoutFromArrayUnifiedSettings){
                [item hiddenLineBottom];
            }
        }
    }
    return self;
}

@end
